package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.util.DateUtils;
import com.itheima.health.vo.SetmealReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itheima.health.dao.ReportDao;
import java.text.SimpleDateFormat;
import java.util.*;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ShiXTong
 * @since 2022/11/22 23:13
 */
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> getBusinessReportData() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        //当前时间
        Date date = new Date();
        //本周起始时间
        Date beginDayOfWeek = DateUtils.getBeginDayOfWeek();

        //本月起始时间
        Date beginDayOfMonth = DateUtils.getBeginDayOfMonth();

        Map<String, Object> map = new HashMap<>();
        //当前时间
        String newDate = dateFormat.format(date);
        //本周起始
        String week = dateFormat.format(beginDayOfWeek);

        //本月起始
        String month = dateFormat.format(beginDayOfMonth);

        //当前日期
        map.put("reportDate", newDate);
        //今日新增会员
        map.put("todayNewMember", memberDao.findTodayNewMember(newDate));
        //总会员数
        map.put("totalMember", memberDao.findTotalMember());
        //本周新增会员
        map.put("thisWeekNewMember", memberDao.findThisWeekNewMember(week));
        //本月新增会员
        map.put("thisMonthNewMember", memberDao.findThisMonthNewMember(month));


        //今日预约数
        map.put("todayOrderNumber", orderDao.findTodayOrderNumber(newDate));
        //今日到诊数
        map.put("todayVisitsNumber", orderDao.findTodayVisitsNumber(newDate));
        //本周预约数
        map.put("thisWeekOrderNumber", orderDao.findThisWeekOrderNumber(week));
        //本周到诊数
        map.put("thisWeekVisitsNumber", orderDao.findThisWeekVisitsNumber(week));
        //本月预约数
        map.put("thisMonthOrderNumber", orderDao.findThisMonthOrderNumber(month));
        //本月到诊数
        map.put("thisMonthVisitsNumber", orderDao.findThisMonthVisitsNumber(month));
        //热门套餐
        List<Map> mapList = orderDao.findHotSetmeal();
        map.put("hotSetmeal", mapList);
        return map;
    }

    public Map<String, Object> getMemberReport() {
        //准备返回数据容器
        Map<String, Object> rememberMap = new HashMap<>();
        //准备日历对象
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.MONTH, -12);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        //获得12个对象并装入返回容器
        ArrayList<String> monthList = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            calendar.add(calendar.MONTH, 1);
            Date date = calendar.getTime();
            String month = simpleDateFormat.format(date);
            monthList.add(month);
        }
        //月份装入返回容器
        rememberMap.put("months", monthList);
        //根据月份查询会员数量
        List<Integer> countList = new ArrayList<>();
        monthList.forEach((item) ->{
            int count = reportDao.countByMonth(item + "-31");
            countList.add(count);
        });
        //会员数量装入返回容器
        rememberMap.put("memberCount", countList);
        return rememberMap;

    }
    @Override
    public List<SetmealReportVo> findSetmealCount() {
        return reportDao.countSetmeal();
    }

}

